/*
 * util_time.h
 *
 *  Created on: Mar 22, 2012
 *      Author: lin
 */

#ifndef UTIL_TIME_H_
#define UTIL_TIME_H_
#include <sys/time.h>
#include <time.h>
#include <stdio.h>

static inline void timeval_gmtime_format(struct timeval *time, char *buffer,
        int len) {
    struct tm tm;
    gmtime_r(&time->tv_sec, &tm);
    snprintf(buffer, len, "%04d%02d%02d %02d:%02d:%02d:%03ld",
            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
            tm.tm_sec, time->tv_usec / 1000);
}

static inline void timeval_localtime_format(struct timeval *time, char *buffer,
        int len) {
    struct tm tm;
    localtime_r(&time->tv_sec, &tm);
    snprintf(buffer, len, "%04d%02d%02d %02d:%02d:%02d:%03ld",
            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
            tm.tm_sec, time->tv_usec / 1000);
}
#endif /* UTIL_TIME_H_ */
